package order_service

import (
	"bee/pkg/logging"
	"bee/pkg/util"
	"errors"
	"github.com/go-resty/resty/v2"
	"github.com/tidwall/gjson"
	"time"
)

type Remittance struct {
	AccountName string `json:"accountName"`
	AccountNo   string `json:"accountNo"`
	AmountEnd   string `json:"amountEnd"`
	AmountStart string `json:"amountStart"`
	DateEnd     string `json:"dateEnd"`
	DateStart   string `json:"dateStart"`
	PageNo      int64  `json:"pageNo"`
	PageSize    int64  `json:"pageSize"`
}
func (o Order) QueryRemittance(host string) (remittanceId string, err error) {

	url := host + "/api/opc/pay/queryRemittance"

	headerData := make(map[string]string)
	headerData["Content-Type"] = "application/json;charset=UTF-8"

	remittanceQuery := Remittance{
		AccountName: o.FullName,
		AccountNo:   o.BankID,
		AmountEnd:   "",
		AmountStart: "",
		DateEnd:     "",
		DateStart:   "",
		PageNo:      1,
		PageSize:    20,
	}

	client := resty.New()

	client.SetTimeout(10 * time.Second)

	cookie := util.SetCookie("sso-epctoken", o.Token)

	remittanceResp, _ := client.R().SetHeaders(headerData).
		SetCookie(cookie).
		SetBody(remittanceQuery).
		Post(url)

	remittanceIdList := gjson.GetBytes(remittanceResp.Body(), "data.list")

	logging.ZapLogger.Infow("流水查询", "流水", remittanceIdList, "traceId", o.TraceId)
	length := len(remittanceIdList.Array())
	if length == 0 {
		return "", errors.New("返回流水列表不能为空")
	}

	remittanceId = gjson.Get(remittanceIdList.Array()[0].String(), "remittanceId").String()
	logging.ZapLogger.Infow("【查询流水】", "流水id", remittanceId, "traceId", o.TraceId)

	return remittanceId, nil

}